import java.util.ArrayList;
import java.util.List;

public class ImportVisitor extends  GoParserBaseVisitor<Object>
{
    @Override
    public Object visitSourceFile(GoParser.SourceFileContext ctx)
    {
        List<Import> imports = new ArrayList<>();
        for (GoParser.ImportDeclContext importDecl : ctx.importDecl())
        {
            Import importNode = (Import) visit(importDecl);
            if (importNode != null)
                imports.add(importNode);
        }
        return imports;
    }

    @Override
    public Object visitImportDecl(GoParser.ImportDeclContext ctx)
    {
        Import _import = new Import();
        _import.pathSpec = new ArrayList<>();
        for(GoParser.ImportSpecContext importSpec : ctx.importSpec())
        {
            Import importNode = (Import)visit(importSpec);
            if(importNode.alias != null)
            {
                _import.alias = importNode.alias;
            }
            if(importNode.pathSpec.size() != 0)
            {
                _import.pathSpec.add(importNode.pathSpec.get(0));
            }
        }
        return _import;
    }

    @Override
    public Object visitImportSpec(GoParser.ImportSpecContext ctx)
    {
        Import _import = new Import();
        if(ctx.alias != null)
        {
            _import.alias = new Id(ctx.alias.getText());
        }
        else
            _import.alias = null;
        String name = (String)visit(ctx.importPath());
        _import.pathSpec = new ArrayList<>();
        _import.pathSpec.add(name);
        return _import;
    }

    @Override
    public Object visitImportPath(GoParser.ImportPathContext ctx)
    {
        return ctx.string_().getText();
    }
}
